package com.embhealth.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.constant.MessageConstant;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.entity.Result;
import com.embhealth.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    //获取用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //当Spring security完成认证后，hi将用户信息保存到上下文对象
        User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user!=null){
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }
        return new Result(false,MessageConstant.GET_USERNAME_FAIL);
    }

    /**分页查询全部用户信息
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return userService.findAllUser(queryPageBean);
    }
    /**
     * 添加用户
     */
    @RequestMapping("/add")
    public Result add(Integer formDataRoleid,@RequestBody com.embhealth.pojo.User user){
        try {
            userService.add(formDataRoleid,user);
            return new Result(true,"新增用户成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"新增用户失败");
        }
    }

    /**
     * 根据id查询用户信息用于数据回显
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map<String, Object> user = userService.findById(id);
            return new Result(true,"查询用户信息成功",user);
        }catch (Exception e){
            return new Result(false,"查询用户信息失败");
        }
    }

    /**
     * 更新用户信息
     * @param formDataRoleid
     * @param user
     * @return
     */
    @RequestMapping("edit")
    public Result editUser(Integer formDataRoleid,@RequestBody com.embhealth.pojo.User user){
        try {
            String password = user.getPassword();
            if (password!=null && !password.equals("") && !password.equals("重置密码")){
                Map<String,Object> map=new HashMap<>();
                map.put("id",user.getId());
                map.put("username",user.getUsername());
                map.put("password",user.getId());
                map.put("telephone",user.getTelephone());
                map.put("station",user.getStation());
                map.put("remark",user.getRemark());
                userService.editUser(formDataRoleid,map);
            }else {
                Map<String,Object> map=new HashMap<>();
                map.put("id",user.getId());
                map.put("username",user.getUsername());
                map.put("telephone",user.getTelephone());
                map.put("station",user.getStation());
                map.put("remark",user.getRemark());
                userService.editUser(formDataRoleid,map);
            }
            return new Result(true,"修改用户信息成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改用户信息失败");
        }
    }

    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
            return new Result(true,"删除用户成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }
    }
}
